package ReusableCode;

import java.util.*;

public class SetLogic {
	public static List<int[]> generateSets(int n) {
		ArrayList<int[]> sets = new ArrayList<int[]>();
		long max = (long) Math.pow(n, n);
		
		int counters[] = new int[n];
		
		//Instantiate the array elements.
		for(int i = 0; i < n; i++)
		{
			counters[i] = 1;
		}
		sets.add(counters.clone());
		
		while(sets.size()<max)
		{
			for(int i = n-1; i >=0; i--)
			{
				if(counters[i]<n)
				{
					counters[i] = counters[i]+1;
					break;
				}
				else 
				{
					counters[i]= 1;
				}		
			}
			
			sets.add(counters.clone());
		}
		
		return sets;
	}
	
	public static Set<Set<Integer>> getSubsets(int[] nums)
	{
		Set<Integer> set = new HashSet<Integer>();
		set.addAll(MathFunctions.arrayToList(nums));
		
		return powerSet(set);
	}
	
	public static Set<Set<Integer>> powerSet(Set<Integer> originalSet) 
	{
        Set<Set<Integer>> sets = new HashSet<Set<Integer>>();
        if (originalSet.isEmpty()) {
            sets.add(new HashSet<Integer>());
            return sets;
        }
        List<Integer> list = new ArrayList<Integer>(originalSet);
        Integer head = list.get(0);
        Set<Integer> rest = new HashSet<Integer>(list.subList(1, list.size()));
        for (Set<Integer> set : powerSet(rest)) {
            Set<Integer> newSet = new HashSet<Integer>();
            newSet.add(head);
            newSet.addAll(set);
            sets.add(newSet);
            sets.add(set);
        }
        
        return sets;
    }
	
	public static long[] getContiguousSums(int n) {
		long max = (long) Math.pow(n, n);
		long size = 0;
		long values[] = new long[n];
		
		int counters[] = new int[n];
		
		//Instantiate the array elements.
		for(int i = 0; i < n; i++)
		{
			counters[i] = 1;
			values[i] = 0;
		}
		
		while(size<max)
		{
			for(int i = n-1; i >=0; i--)
			{
				if(counters[i]<n)
				{
					counters[i] = counters[i]+1;
					break;
				}
				else 
				{
					counters[i]= 1;
				}		
				
				
			}
			
			int count = 0, num = 0, max2 = 1;
			for(int i = 0; i < n; i++)
			{
				//Handle the series.
				if(counters[i] == num)
				{
					count++;
					if(count>max2)
					{
						max2 = count;
					}
				}
				else
				{
					count=1;
					num = counters[i];
				}
			}
			values[max2-1] = values[max2-1]+1;
			
			size++;
		}
		
		return values;
	}
}
